package com.espirit.hr;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.googlecode.cswish.struts.spring.GenericService;
import com.googlecode.cswish.util.bean.ModelUtil;

@Service
public class HrSettingService {
	@Resource
	GenericService genericService;
	
	public void _update(HrSetting model){
		HrSetting hrSetting = getHrSetting();
		if (hrSetting == null){
			hrSetting = new HrSetting();
			hrSetting.setSchedualDeptIds("");
			hrSetting = genericService.add(hrSetting);
		} 
		ModelUtil.copy(hrSetting, model);
	}

	public String[] getSchedualDepartmentIds() {
		HrSetting hrSetting = getHrSetting();
		if (hrSetting != null){
			String ids = hrSetting.getSchedualDeptIds();
			if (!StringUtils.isEmpty(ids)) {
				return ids.split(",");
			}
		}
		return null;
	}
	
	public HrSetting getHrSetting(){
		HrSetting example = new HrSetting();
		example = genericService.searchAll(example).getData(0);
		if (example == null){
			example = new HrSetting();
			example.setSchedualDeptIds("");
			example = genericService.add(example);
		} 
		return example;
	}
}
